package team7.made;

import java.io.IOException;

import android.app.Activity;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import android.view.ViewGroup.LayoutParams;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class main<HelloFormStuff> extends Activity {
   	
	// Global variables for demo test
	String answer;
	boolean submitted = false;
	TestSession protoTest;
    List<Question> questionList;
    Iterator<Question> testIterator;
    Question currentQuestion;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        //Parse and initialize
        parseQuestions();
        protoTest = new TestSession(3);
        questionList = protoTest.startTest("ProtoType Test");
        testIterator = questionList.iterator();
        currentQuestion = testIterator.next();
        
        // draw question
        displayQuestion(currentQuestion);       
    }
    
    public void nextQuestion(){
    	protoTest.setResult(currentQuestion, answer);
    	if(testIterator.hasNext()){
    		currentQuestion = testIterator.next();
    		displayQuestion(currentQuestion);
    	}
    	else{
    		TextView resultsView = new TextView(this);
    		resultsView.setText("Correct: " + protoTest.getNumberOfQuestionsCorrect() + "\n" + "Incorrect: " + protoTest.getNumberOfQuestionsIncorrect());
    		setContentView(resultsView);
    	}
    }
    
    // Function parsing questions file, should only be called in onCreate()
    public void parseQuestions(){
        try {
			QuestionsDBase.INSTANCE.init(getAssets().open("Questions.xml"));
		} catch (IOException e) {
			// exit application displaying error message.
			String errMsg = "Could not load Education material onto app. Contact tech-support [team7@uncc.com]";
			Toast.makeText(this, errMsg, Toast.LENGTH_LONG).show();
			// log message onto log-cat
			android.util.Log.e("MADE", errMsg + " error[" + e.getMessage() + "]");
		}
    }
    
    // Draws questions (and form for answering)
    public void displayQuestion(Question question){
    	// Define the layout attributes
    	Display display = getWindowManager().getDefaultDisplay();
    	ViewGroup.LayoutParams qvParams = new ViewGroup.LayoutParams(display.getWidth()*4/5,ViewGroup.LayoutParams.MATCH_PARENT);
    	
    	// Instantiate
    	TextView questionView = new TextView(this);
    	
    	// Assemble question String
    	Object[] options = question.getOptions().toArray();
    	
    	// TO FIX:
    	// options[1] = A, options[2] = B, options[3] = C options[0] = D
    	String questionString = question.getQuestionStatement() + "\n" + options[1] + "\n" + options[2] + "\n" + options[3] + "\n" + options[0];
    	
    	// Assign question text
    	questionView.setText(questionString);
    	// Draw question template view (this also destroys existing views)
    	setContentView(R.layout.questionview);
    	// Draw question
    	addContentView(questionView, qvParams);
    	// Initialize buttons
    	initButtons();
    }
    
    

    public void initButtons(){
    	final RadioButton radio_a = (RadioButton) findViewById(R.id.radio_answera);
        final RadioButton radio_b = (RadioButton) findViewById(R.id.radio_answerb);
        final RadioButton radio_c = (RadioButton) findViewById(R.id.radio_answerc);
        final RadioButton radio_d = (RadioButton) findViewById(R.id.radio_answerd);
        final Button nextButton = (Button) findViewById(R.id.btn_questionsubmit);
        radio_a.setOnClickListener(radioListenerA);
        radio_b.setOnClickListener(radioListenerB);
        radio_c.setOnClickListener(radioListenerC);
        radio_d.setOnClickListener(radioListenerD);
        nextButton.setOnClickListener(nextListener);
    }
    
    // Test listeners
    // THIS FORMAT IS DEPRECATED
    private OnClickListener radioListenerA = new OnClickListener() {
        @Override
    	public void onClick(View v) {
            // Perform action on clicks
            RadioButton rb = (RadioButton) v;
            setAnswer("A");
        }
    };
    private OnClickListener radioListenerB = new OnClickListener() {
        @Override
    	public void onClick(View v) {
            // Perform action on clicks
            RadioButton rb = (RadioButton) v;
            setAnswer("B");
        }
    };
    private OnClickListener radioListenerC = new OnClickListener() {
        @Override
    	public void onClick(View v) {
            // Perform action on clicks
            RadioButton rb = (RadioButton) v;
            setAnswer("C");
        }
    };
    private OnClickListener radioListenerD = new OnClickListener() {
        @Override
    	public void onClick(View v) {
            // Perform action on clicks
            RadioButton rb = (RadioButton) v;
            setAnswer("D");
        }
    };
    private OnClickListener nextListener = new OnClickListener() {
        @Override
    	public void onClick(View v) {
            // Perform action on clicks
            Button b = (Button) v;
            nextQuestion();
        }
    };
    
    // Test set answer
    public void setAnswer(String string){
    	answer = string;
    }
}